<?php

namespace app\common\model;

use think\Model;

class Member extends Model
{
    public function add($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('add')->check($data)) {
            return $validate->getError();
        }
        $result = $this->save($data);
        if ($result) {
            return 1;
        }else {
            return '会员添加失败！';
        }
    }

    public function edit($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('edit')->check($data)) {
            return $validate->getError();
        }
        $oldpassword = $this->where('id', $data['id'])->value('password');
        if ($data['oldpassword'] != $oldpassword) {
            return '原密码不正确！';
        }
        $data['password'] = $data['newpassword'];
        $result = $this->isUpdate(true)->save($data);
        if ($result) {
            return 1;
        }else {
            return '会员修改失败！';
        }
    }

    public function login($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('login')->check($data)) {
            return $validate->getError();
        }
        unset($data['verify']);
        $result = $this->where($data)->find();
        if ($result) {
            session('username', $data['username']);
            return 1;
        }else {
            return '用户名或者密码错误！';
        }
    }

    public  function register($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('register')->check($data)) {
            return $validate->getError();
        }
        $result = $this->allowField(true)->save($data);
        if ($result) {
            return 1;
        }else {
            return '注册失败！';
        }
    }
}
